#include "family.h"
//计算树的高度
int FamilyTree::calculateHeight(FamilyMember* node) {
    if (node == nullptr) return 0;

    int max_height = 0;
    for (auto child : node->children) {
        int child_height = calculateHeight(child);
        if (child_height > max_height) {
            max_height = child_height;
        }
    }

    return max_height + 1;
}

//检查树是否平衡
bool FamilyTree::isBalanced(FamilyMember* node) {
    if (node == nullptr) return true;

    int left_height = (node->children.size() > 0) ? calculateHeight(node->children[0]) : 0;
    int right_height = (node->children.size() > 1) ? calculateHeight(node->children[1]) : 0;

    if (abs(left_height - right_height) > 1) return false;

    return isBalanced(node->children.size() > 0 ? node->children[0] : nullptr) &&
           isBalanced(node->children.size() > 1 ? node->children[1] : nullptr);
}
